package com.vmove.childtrain.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.vmove.childtrain.R;
import com.vmove.childtrain.utils.AutoUtils;
import com.vmove.childtrain.utils.BitmapUtil;

import androidx.appcompat.widget.DrawableUtils;


/**
 * 使用layout拖拽移动 只能在parentView中移动，超出后显示不全
 */
public class LightView extends RelativeLayout {

    private View rootView;
    private DoughnutProgress mViewLight;
//    private TextView mTextView;
    private ImageView mIvNumber;
    private Context context;

    public LightView(Context context) {
        super(context);
        this.context = context;
        init(null, 0);
    }

    public LightView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs, 0);
    }

    public LightView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        rootView = LayoutInflater.from(getContext()).inflate(R.layout.view_lightview, this);
        mIvNumber = rootView.findViewById(R.id.iv_number);
        mViewLight = (DoughnutProgress) rootView.findViewById(R.id.doughnut);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.LightView);
//        String number="0";
//        if (a.hasValue(R.styleable.LightView_numberString))
//             number= a.getString(R.styleable.LightView_numberString);
//        setLightNumber(number);
    }

    public void setLightNumber(String number) {
        setLightNumber(Integer.parseInt(number));
    }

    public void setLightNumber(int number) {
        switch (number){
            case 1:
            case 9:
                mIvNumber.setImageResource(R.drawable.ic_one);
//                mIvNumber.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_one116));
                break;
            case 2:
            case 10:
                mIvNumber.setImageResource(R.drawable.ic_two);
//                mIvNumber.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_two116));
                break;
            case 3:
            case 11:
                mIvNumber.setImageResource(R.drawable.ic_three);
//                mIvNumber.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_three116));
                break;
            case 4:
            case 12:
                mIvNumber.setImageResource(R.drawable.ic_four);
//                mIvNumber.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_four));
                break;
            case 5:
            case 13:
                mIvNumber.setImageResource(R.drawable.ic_five);
//                mIvNumber.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_five));
                break;
            case 6:
            case 14:
                mIvNumber.setImageResource(R.drawable.ic_six);
//                mIvNumber.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_six));
                break;
            case 7:
            case 15:
                mIvNumber.setImageResource(R.drawable.ic_seven);
//                mIvNumber.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_seven));
                break;
            case 8:
            case 16:
                mIvNumber.setImageResource(R.drawable.ic_eight);
//                mIvNumber.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_eight));
                break;
        }
        if (number<=8){
            mViewLight.setOnGroup(1);
        }else {
            mViewLight.setOnGroup(2);
        }
    }

    public void setStatus(boolean on) {
        mViewLight.setStatus(on);
    }

    public void setElectricLevel(int level){
        mViewLight.setElectricLevel(level);
        mViewLight.setStatus(true);
    }
}
